/**
 * 进入玩法房间
 */
var RoomBoard = Board.extend({
    room_state_created: 1,
    room_state_ongoing: 2,
    room_state_ended: 3,
    _assistantWindow: null,
    _pokerLayer: null,
    _ccsFileName: res.RoomBoard_json,
    initBoard: function (data, roomDataType) {
        hideLoading();
        var key = findKey(app.map_id, gameData.mapId);
        if (!key) {
            cc.error('mapId:' + gameData.mapId + ' 找不到对应的玩法');
            return;
        }
        var sub = app.map_id_2_sub[key];
        if (!sub) {
            cc.error(key + ' 找不到对应的sub');
            return;
        }
        if (!window['SubGameLayer']) {
            cc.error('缺少玩法启动Layer');
            return;
        }
        /**
         * 创建游戏场景
         */
        var gameLayer = new window['SubGameLayer'];
        this.addChild(gameLayer);
        /**
         * 统一添加表情组件
         */
        var roomLayer = gameLayer.initGameLayer(data, roomDataType);

        gameData.RoomStateIsReplay = (roomDataType == ROOM_DATA_TYPE.REPLAY);
        if (!gameData.RoomStateIsReplay) {
            ClubDataManager.ClubRecordData = null;
        }

        if (roomLayer && roomLayer.getChatInfoNode) {
            EmojiUtil.initUtil(roomLayer);
            this._initClubAssistant(roomLayer, sub);
            // this._broadcastUidListener = cc.eventManager.addCustomListener('BroadcastUid', this._clubCmdBroadcastUid.bind(this));
        }
        ClubViewUtil.hideClubTableWindow();
    },
    
    _onSceneTransitionOver: function () {
        Board.prototype._onSceneTransitionOver.call(this);
        eventManager.dispatchEvent(EventKey.ROOM_BOARD_TRANSITION_OVER)
    },


    /**
     * 退出处理
     */
    // onExit: function () {
    //     Board.prototype.onExit.call(this);
    //     if (this._broadcastUidListener) {
    //         cc.eventManager.removeListener(this._broadcastUidListener);
    //     }
    // },


    /**
     * 初始化俱乐部小组手
     * @private
     */
    _initClubAssistant: function (roomLayer, sub) {
        this._pokerLayer = roomLayer;
        if (gameData.roomClubId && gameData.roomClubId > 0) {
            var roomState = false;
            if (this._pokerLayer && _.isFunction(this._pokerLayer.getRoomState)) {
                roomState = this._pokerLayer.getRoomState() == this.room_state_created;
            }
            if (roomState && gameData.players.length < gameData.playerNum) {
                // SubUpdateUtil.checkUpdateSubByMapId(app.map_id.CLUB, function () {
                    if (this && cc.sys.isObjectValid(this) && roomState && gameData.players.length < gameData.playerNum) {
                        mRoom.club_id = gameData.roomClubId;
                        // this._assistantWindow = new ClubAssistant(mRoom.club_id);
                        // this.addChild(this._assistantWindow, 100);
                        //#region 暂时屏蔽小助手
                        this._assistantWindow = addUnit('ClubAssistantUnit',this,gameData.options['club_id']);
                        //#endregion
                    }
                // }.bind(this), true);
            }
        }
    },

    _initEventListeners: function () {
        Board.prototype._initEventListeners.call(this);
        this._eventListeners[EventKey.ClubPlayerEnterExit] = '_playerEnterExit';
        this._eventListeners[EventKey.ClubRoomStateChange] = '_roomStateChange';
        this._eventListeners[EventKey.ClubRoomGameStart] = '_roomStart';
    },

    _initNetworkListeners: function () {
        Unit.prototype._initNetworkListeners.call(this);
        this._networkListeners = {
            ClubOpt: '_onClubOpt',
            SubUpdate: '_subUpdate',
            MatchOpt: '_onMatchOpt',
            GoldOpt: '_onGoldOpt',
        };
    },

    /**
     * 俱乐部消息反馈
     * @param data
     * @private
     */
    _onClubOpt: function (data) {
        if (data) {
            var cmd = data['command'];
            var errorCode = data['result'];
            var errorMsg = data['msg'];

            if (errorCode && errorMsg != '没有房间') {
                showAlert(errorMsg, 'noAnimation');
            }
            data.scene = 'pdk';
            // cc.log('发送出去的消息:' + cmd);
            cc.eventManager.dispatchCustomEvent(cmd, data);
        }
    },

    /**
     * 模块更新
     * @private
     */
    _subUpdate: function (data) {
        SubUpdateUtil.subUpdate(data);
    },
    /**
     * 比赛场协议监听
     */
    _onMatchOpt: function (data) {
        if (data && data['cmd']) {
            cc.eventManager.dispatchCustomEvent('match_' + data['cmd'], data);
        }
    },
    /**
     * 金币场协议监听
     */
    _onGoldOpt: function (data) {
        if (data && data['cmd']) {
            cc.eventManager.dispatchCustomEvent('jbc_' + data['cmd'], data);
        }
    },
    /**
     * 玩家进入或者退出房间时 刷新小助手显示
     * @param {Array} players
     * @private
     */
    _playerEnterExit: function (players) {
        var roomStateIsCreate = false;
        if (this._pokerLayer && _.isFunction(this._pokerLayer.getRoomState)) {
            roomStateIsCreate = this._pokerLayer.getRoomState() == this.room_state_created;
        }
        if (gameData.roomClubId && gameData.roomClubId > 0 && this._assistantWindow && roomStateIsCreate && cc.sys.isObjectValid(this._assistantWindow)) {
            this._assistantWindow.setVisible(players.length < gameData.playerNum);
        }
    },
    /**
     * 牌桌房间状态改变
     * @private
     */
    _roomStateChange: function (roomState) {
        if (this._assistantWindow && cc.sys.isObjectValid(this._assistantWindow)) {
            this._assistantWindow.setVisible(roomState == this.room_state_created);
        }
    },

    /**
     * 牌局开始
     * @private
     */
    _roomStart: function () {
        var clubTablesLayer = this.getChildByName('clubTablesLayer');
        if (clubTablesLayer && cc.sys.isObjectValid(clubTablesLayer)) {
            clubTablesLayer.removeFromParent(true);
        }
    },


    /**
     * 监听俱乐部BroadcastUid消息操作
     * @param event
     * @private
     */
    _clubCmdBroadcastUid: function (data) {
        // var data = event.getUserData();
        if (data.scene == 'home') return;
        if (data) {
            var message = data['data'];
            if (message) {
                var type = message['type'] || '';
                if (type == MessageType.Invite) {
                } else if (type == MessageType.Refused) {
                    if (this._pokerLayer && _.isFunction(this._pokerLayer.getRoomState)) {
                        var roomState = this._pokerLayer.getRoomState() == this.room_state_ongoing;
                        if (!roomState) {
                            showAlert(message['msg']);
                        }
                    }
                }
            }
        }
    },


});